package com.hanbang.maige.view.quanzi.activity;

import android.content.Context;
import android.content.Intent;
import android.support.design.widget.TextInputLayout;
import android.support.v7.widget.Toolbar;
import android.view.MenuItem;
import android.view.View;
import android.widget.GridLayout;
import android.widget.TextView;

import com.hanbang.maige.R;
import com.hanbang.maige.code.base.view.activity.BaseMvpActivity;
import com.hanbang.maige.mode.enumeration.ShowQuanXian;
import com.hanbang.maige.mode.javabean.quanzi.FaBuFriendData;
import com.hanbang.maige.mode.javabean.quanzi.PoiData;
import com.hanbang.maige.mode.javabean.quanzi.QuanZiQuanXianData;
import com.hanbang.maige.pressenter.quanzi.FaBuFriendPressenter;
import com.hanbang.maige.utils.other.EditHelper;
import com.hanbang.maige.utils.other.Validators;
import com.hanbang.maige.view.quanzi.IQuanZiBaseView;
import com.hanbang.maige.view.quanzi.adapter.FaBuNineGridAdapter;
import com.hanbang.maige.widget.gridlayout.SuperGridLayout;
import com.hubng.photo_hander.PhotoHander;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 作者　　: 李坤
 * 创建时间:2016/10/31　12:11
 * 邮箱　　：496546144@qq.com
 * <p>
 * 功能介绍：发布朋友圈activity
 */

public class FaBuFriendActivity extends BaseMvpActivity<IQuanZiBaseView.IFaBuFriendView, FaBuFriendPressenter>
        implements IQuanZiBaseView.IFaBuFriendView, Toolbar.OnMenuItemClickListener {
    @BindView(R.id.contentTil)
    TextInputLayout contentTil;
    @BindView(R.id.imageNgiv)
    SuperGridLayout imageNgiv;
    @BindView(R.id.weizhiTv)
    TextView weizhiTv;
    @BindView(R.id.quanxianTv)
    TextView quanxianTv;
    private FaBuFriendData faBuFriendData = new FaBuFriendData();

    FaBuNineGridAdapter adapter;

    private EditHelper editHelper = new EditHelper(this);


    public static void startUI(Context context) {
        Intent intent = new Intent(context, FaBuFriendActivity.class);
        context.startActivity(intent);
    }

    @Override
    public FaBuFriendPressenter initPressenter() {
        return new FaBuFriendPressenter();
    }

    @Override
    public int getContentView() {
        return R.layout.activity_fabu_friend;
    }

    @Override
    public void initView() {
        toolbar.setBack(this);
        toolbar.addAction(0, "发送");
        toolbar.setOnMenuItemClickListener(this);

        faBuFriendData.photoAddIcon();

        adapter = new FaBuNineGridAdapter(this, faBuFriendData.getFriendPhoto(), imageNgiv);
        imageNgiv.setAdapter(adapter);
        weizhiTv.setText(faBuFriendData.getPoiData() == null ? "所在位置" : faBuFriendData.getPoiData().getTitle());
        quanxianTv.setText(faBuFriendData.getShowQuanXian().getValuse());
        editHelper.addEditHelperData(new EditHelper.EditHelperData(contentTil, Validators.getLengthSRegex(1, 300), "您不想说点什么吗?"));


        imageNgiv.setOnItemClickListener(new SuperGridLayout.OnItemClickListener<String>() {
            @Override
            public void onItemClick(GridLayout parent, View view, int position, String data) {
                if ("add".equals(faBuFriendData.getFriendPhoto().get(position))) {
                    faBuFriendData.photoRemove();
                    PhotoHander.create().multi().compress(true).count(9).origin(faBuFriendData.getFriendPhoto()).showCamera(true)
                            .start(FaBuFriendActivity.this, REQUEST_CODE);
                } else {
                    faBuFriendData.photoRemove();
                }
            }
        });
    }

    @Override
    public void parseIntent(Intent intent) {

    }

    @Override
    public void clearData() {

    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == REQUEST_CODE && resultCode == RESULT_OK) {//照片选择
            faBuFriendData.setFriendPhoto(data.getStringArrayListExtra(PhotoHander.EXTRA_RESULT));
            faBuFriendData.photoAddIcon();
            adapter.setmDatas(faBuFriendData.getFriendPhoto());
            adapter.notifyDataSetChanged();
            faBuFriendData.setFriendPhoto(faBuFriendData.getFriendPhoto());
        } else if (requestCode == REQUEST_CODE + 1 && resultCode == RESULT_OK) {//权限选择
            QuanZiQuanXianData quanXianData = data.getParcelableExtra("data");
            if (quanXianData != null) {
                faBuFriendData.setShowQuanXian(ShowQuanXian.getState(quanXianData.getKey()));
                quanxianTv.setText(faBuFriendData.getShowQuanXian().getValuse());
            }
        } else if (requestCode == REQUEST_CODE + 2 && resultCode == RESULT_OK) {//位置选择
            PoiData poiData = data.getParcelableExtra("data");
            faBuFriendData.setPoiData(poiData);
            weizhiTv.setText(poiData == null ? "所在位置" : poiData.getTitle());
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        faBuFriendData.photoAddIcon();

    }

    @OnClick({R.id.weizhiTv, R.id.quanxianRL})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.weizhiTv:
                LocationSelectActivity.startUI(FaBuFriendActivity.this, faBuFriendData.getPoiData());
                break;
            case R.id.quanxianRL:
                QuanZiQuanXianActivity.startUI(FaBuFriendActivity.this);
                break;
        }
    }

    @Override
    public void updataUI(String msg) {
        finish();
    }

    @Override
    public boolean onMenuItemClick(MenuItem item) {

        if (!editHelper.check()) {
            return false;
        }
        faBuFriendData.setContent(editHelper.getText(R.id.contentTil));

        presenter.getHttpData(faBuFriendData);

        return true;
    }
}
